<?php

namespace App\Http\Controllers\Guard\Order;

use App\Exports\LiPeiExport;
use App\Exports\OrderExport;
use App\Http\Controllers\Common\GuardController;
use App\Models\Constant\OrderConstant;
use App\Models\Order\Order;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class OrderController extends GuardController {


    public function index(Request $request) {
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);
        $conditions = $request->input();
        $repairFactoryName = $request->input('repairFactoryName');
        $useWay = $request->input('use_way',0);
        if($conditions['status']==OrderConstant::STATUS_WAIT_AUTO_CHECK){
            /*$conditions['status'] = [
                OrderConstant::STATUS_WAIT_AUTO_CHECK,
                OrderConstant::STATUS_WAIT_HUMAN_CHECK,
                OrderConstant::STATUS_WAIT_AUTO_HUMAN_CHECK
            ];*/
        }

        $records = (new Order())->getLst($conditions)->with('getRepairFactory');
        if(!empty($repairFactoryName)){
            $records = $records->whereHas('getRepairFactory',function ($q) use ($repairFactoryName){
                $q->where('name','like',"%$repairFactoryName%");
            });
        }

        $records = $records->paginate($pageSize,$this->getCol,'page',$page);
        $mapStatus = OrderConstant::MAP_STATUS;
        foreach ($records as $record) {
            $record->repair_way = $record->repair_factory_id==0?'自主维修':'指定维修';
            $record->car_jia_number_of_cheng_bao = $record->getCarJiaNumberOfChengBao();
            $record->status_name = $mapStatus[$record->status]??'未知';
        }
        $mapStatus = [
            ['value'=>0,'label'=>'全部'],
            ['value'=>2,'label'=>'待审核'],
            ['value'=>5,'label'=>'待报销'],
            ['value'=>6,'label'=>'已完成'],
            ['value'=>10,'label'=>'取消'],
            ['value'=>11,'label'=>'未通过'],
        ];


        /*foreach (OrderConstant::MAP_STATUS as $k=>$v) {

            if(in_array($k, [OrderConstant::STATUS_WAIT_HUMAN_CHECK,OrderConstant::STATUS_WAIT_AUTO_HUMAN_CHECK]))
                continue;
            $mapStatus[] = ['value'=>$k, 'label'=>$v];
        }*/


        return responseSuccess($records->items(),$records->total(),100000,compact('mapStatus'));
    }

    public function secondPass(Request $request) {
        $id = $request->input('id');
        $status = OrderConstant::STATUS_CHECK_PASS;
        $reason = null;
        $ocr_reason = null;
        $lipei_reason = null;
        //$info = compact('status','reason','ocr_reason','lipei_reason');
        if(!is_array($id)){
            $order = Order::where('id',$id)->first();
            if(!empty($order)){
                $order->status = $status;
                $order->reason = $reason;
                $order->ocr_reason = $ocr_reason;
                $order->lipei_reason = $lipei_reason;
                $order->save();
            }

        }else{
            foreach ($id as $item) {
                $order = Order::where('id',$item)->first();
                if(!empty($order)){
                    $order->status = $status;
                    $order->reason = $reason;
                    $order->ocr_reason = $ocr_reason;
                    $order->lipei_reason = $lipei_reason;
                    $order->save();
                }
            }
        }

        return responseSuccess();
    }

    public function secondNotPass(Request $request) {
        $id = $request->input('id');
        $reason = $request->input('reason');
        $this->validate($request, ['reason'=>'required'],[],['reason'=>'拒绝理由']);

        $status = OrderConstant::STATUS_CHECK_NOT_PASS;
        $order = Order::where('id',$id)->first();
        $order->reason = $reason;
        $order->status = $status;
        $order->save();

        //Order::where(compact('id'))->update(compact('reason','status'));
        return responseSuccess();
    }

    public function repayPass(Request $request) {
        $id = $request->input('id');
        $status = OrderConstant::STATUS_WAIT_REPAY;
        //$status = OrderConstant::STATUS_CHECK_PASS;
        $reason = null;
        $ocr_reason = null;
        $lipei_reason = null;
        //$info = compact('status','reason','ocr_reason','lipei_reason');
        if(is_array($id)){
            foreach ($id as $item) {
                $order = Order::where('id',$item)->first();
                if(!empty($order)){
                    $order->status = $status;
                    $order->reason = $reason;
                    $order->ocr_reason = $ocr_reason;
                    $order->lipei_reason = $lipei_reason;
                    $order->save();
                }
            }
        }else{
            $order = Order::where('id',$id)->first();
            if(!empty($order)){
                $order->status = $status;
                $order->reason = $reason;
                $order->ocr_reason = $ocr_reason;
                $order->lipei_reason = $lipei_reason;
                $order->save();
            }
        }
        return responseSuccess();
    }

    public function repayNotPass(Request $request) {
        $id = $request->input('id');
        $reason = $request->input('reason');

        $this->validate($request, ['reason'=>'required'],[],['reason'=>'拒绝理由']);

        //$status = OrderConstant::STATUS_WAIT_OFF;
        //$status = OrderConstant::STATUS_CHECK_PASS;
        $status = OrderConstant::STATUS_CHECK_NOT_PASS;
        $order = Order::where('id',$id)->first();
        $order->reason = $reason;
        $order->status = $status;
        $order->save();
        return responseSuccess();
    }

    public function repairedNotPass(Request $request) {
        $id = $request->input('id');
        $reason = $request->input('reason');

        $this->validate($request, ['reason'=>'required'],[],['reason'=>'拒绝理由']);

        $status = OrderConstant::STATUS_WAIT_REPAIRING;
        Order::where(compact('id'))->update(compact('reason','status'));
        return responseSuccess();
    }

    public function end(Request $request) {
        $id = $request->input('id');
        $status = OrderConstant::STATUS_END;
        $reason = null;
        if(is_array($id)){
            foreach ($id as $item) {
                $order = Order::where('id',$item)->first();
                if(!empty($order)){
                    $order->status = $status;
                    $order->reason = $reason;
                    $order->save();
                }
            }
        }else{
            $order = Order::where('id',$id)->first();
            if(!empty($order)){
                $order->status = $status;
                $order->reason = $reason;
                $order->save();
            }
        }
        return responseSuccess();
    }

    public function exportFile(Request $request) {
        $conditions = $request->input();
        $conditions['orderBy']['col'] = 'created_at';
        $conditions['orderBy']['sort'] = 'desc';
        return Excel::download(new OrderExport($conditions),'订单.xlsx');
    }
}
